import {FormGroup, Validators} from '@angular/forms';



// p-calendar语言本地化
export const Es = {
  firstDayOfWeek: 1,
  dayNames: ['domingo', 'lunes', 'martes', 'miércoles', 'jueves', 'viernes', 'sábado'],
  dayNamesShort: ['dom', 'lun', 'mar', 'mié', 'jue', 'vie', 'sáb'],
  dayNamesMin: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
  monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
  monthNamesShort: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
  dateFormat: 'yy-mm-dd',
  today: '当前时间',
  clear: '清除'
};

/**
 * 树形结构数据平行序列化函数
 * @param data 需要平行序列化的数据
 */
export function reverseTree(data): Array<any> {
  let queen = [];
  const out = [];
  queen = queen.concat(data);
  while (queen.length) {
    const first = queen.shift();
    if (first.children) {
      queen = queen.concat(first.children);
      delete first.children;
    }
    out.push(first);
  }
  return out;
}

/**
 * 对象赋值
 * @param assignedObj Object 需要赋值的对象
 * @param copyObj Object 被复制的对象
 * @returns {*}
 */
export function objectCopy(assignedObj: any, copyObj: any): any {
  const obj = {};
  for (const prop in assignedObj) {
    if (assignedObj.hasOwnProperty(prop)) {
      obj[prop] = copyObj[prop];
    }
  }
  return obj;
}

// 下拉框数据转换
export function  setDrapdownOptionList(list: Array<any>): any {
  return list.map(val => {
    return {label: val.settingName, value: val.settingCode};
  });
}

/**
 * formData的图像列表
 * @param data formdata 动态表单对象
 * @param label formdata 的文件参数名
 * @param formData formdata对象
 */
export function setImageToFromData(data: FormGroup , label: string, formData: FormData) {
  console.log(data);
  if (data.value[label] && data.value[label] !== ''){
    data.value[label].forEach(val => {
      formData.append(label, val);
    });
  }else {
    formData.append(label, '');
  }
}

export function setVlaueToLabel(list: Array<any>, data: string){
   list.forEach(val => {
     if (val.value === data){
       data = val.label;
     }
   });
   return data;
}

export function setLabelToVlaue(list: Array<any>, data: string){
  list.forEach(val => {
    if (val.label === data){
      data = val.value;
    }
  });
  return data;
}

// 给表单赋值
export function setValueToFromValue(list: Array<string>, data: object, formGroup: FormGroup) {
  const obj = {};
  list.forEach(val => {
    obj[val] = data[val];
    formGroup.patchValue({[val]: data[val]});
  });
}

export function InitFormGroup(obj: any, option = []): any  {
  const a = Object.assign(obj);
  for (const keys in a) {
    if (a.hasOwnProperty(keys)) {
      if (option.includes(keys)) {
        a[keys] = [''];
      } else {
        a[keys] = ['', Validators.required];
      }
    }
  }
  return a;
}

/**
 * 去除数组中重复的项
 * @param arr
 */
export function rmRepeatArray(arr: Array<any>): Array<any> {
  const oldArr = [];
  const newArr = [];
  const resArr = [];
  for (const item of arr) {
    oldArr.push(item.permissionId);
  }
  oldArr.forEach((item, i) => {
    if (newArr.indexOf(oldArr[i]) < 0) {
     newArr.push(oldArr[i]);
   }
  });
  newArr.forEach((item, i) => {
    resArr.push({permissionId: item});
  });
  return resArr;
}

/**
 * 将base64转成File
 * @param urlData
 * @param filename
 */
function base64ToFile(urlData, filename) {
  if (typeof urlData !== 'string') {
    return false;
  }
  const arr = urlData.split(',');
  const type = arr[0].match(/:(.*?);/)[1];
  const fileExt = type.split('/')[1];
  const bstr = atob(arr[1]);
  let n = bstr.length;
  const u8arr = new Uint8Array(n);
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n);
  }
  return new File([u8arr], `${filename}.` + fileExt, {
    type: type
  });
}

/**
 * 将Uint8Array转换成base64
 * @param u8Arr Uint8Array对象
 */
function uint8arrayToBase64(u8Arr) {
  const CHUNK_SIZE = 0x8000;
  let index = 0;
  const length = u8Arr.length;
  let result = '';
  let slice;
  while (index < length) {
    slice = u8Arr.subarray(index, Math.min(index + CHUNK_SIZE, length));
    result += String.fromCharCode.apply(null, slice);
    index += CHUNK_SIZE;
  }
  return 'data:image/jpg;base64,' + btoa(result);
}

/**
 * 将符合字节流的string转化成Uint8Array
 * @param {String} data
 * @return {Blob}
 * @api public
 */
function binaryToUint8Array(data) {
  const arr = new Uint8Array(data.length);
  for (let i = 0, l = data.length; i < l; i++) {
    arr[i] = data.charCodeAt(i);
  }
  return arr;
}

/**
 * 根据URL获取图片的并转成File对象
 * @param {String} url 需要请求的图片地址
 * @param fileName 图片的文件名
 * @return {File} 返回一个File对象
 */
export function getImageFile(url, fileName) {
  const r = new XMLHttpRequest();
  r.open('GET', url, false);
  r.overrideMimeType('text/plain; charset=x-user-defined');
  r.send(null);
  const unit8Arr = binaryToUint8Array(r.responseText);
  const base64 = uint8arrayToBase64(unit8Arr);
  return base64ToFile(base64, fileName);
}

// tslint:disable-next-line:variable-name
export function DateAdd(interval, number, date) {
  /*
   *---------------  DateAdd(interval,number,date)  -----------------
   *  DateAdd(interval,number,date)
   *  功能:实现VBScript的DateAdd功能.
   *  参数:interval,字符串表达式，表示要添加的时间间隔.
   *  参数:number,数值表达式，表示要添加的时间间隔的个数.
   *  参数:date,时间对象.
   *  返回:新的时间对象.
   *  var  now  =  new  Date();
   *  var  newDate  =  DateAdd( "d ",5,now);
   *  author:wanghr100(灰豆宝宝.net)
   *  update:2004-5-28  11:46
   *---------------  DateAdd(interval,number,date)  -----------------
   */
  switch (interval) {
    case  'y '  : {
      date.setFullYear(date.getFullYear() + number);
      return date;
      break;
    }
    case  'q '  : {
      date.setMonth(date.getMonth() + number * 3);
      return date;
      break;
    }
    case  'm '  : {
      date.setMonth(date.getMonth() + number);
      return date;
      break;
    }
    case  'w '  : {
      date.setDate(date.getDate() + number * 7);
      return date;
      break;
    }
    case  'd '  : {
      date.setDate(date.getDate() + number);
      return date;
      break;
    }
    case  'h '  : {
      date.setHours(date.getHours() + number);
      return date;
      break;
    }
    case  'm '  : {
      date.setMinutes(date.getMinutes() + number);
      return date;
      break;
    }
    case  's '  : {
      date.setSeconds(date.getSeconds() + number);
      return date;
      break;
    }
    default  : {
      date.setDate(date.getDate() + number);
      return date;
      break;
    }
  }
}
